/*
 * Copyright [2009] [Marcin Rzeźnicki]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package mr.go.yaxc.actions;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.LinkedList;

import mr.go.yaxc.Action;
import mr.go.yaxc.ConfigurationError;
import mr.go.yaxc.ScopeHandler;

import org.xml.sax.Attributes;

/**
 * This action creates an array which it passes to parent's <i>params</i>, or,
 * if <i>id</i> attribute is set, puts it in the global context
 * 
 * <h6>on xml element start:</h6> Sets <i>params</i> <h6>on xml element end:</h6>
 * Creates array of <i>type</i> and fills this array with <i>params</i><h6>
 * prerequisites:</h6> none <h6>attributes:</h6>
 * <table>
 * <tr>
 * <th>Attribute name</th>
 * <th>What is it for?</th>
 * <th>Required?</th>
 * </tr>
 * <tr>
 * <td><i>type</i></td>
 * <td>component type of created array</td>
 * <td>yes</td>
 * </tr>
 * <tr>
 * <td><i>id</i></td>
 * <td>if set, puts created array in global context</td>
 * <td>no</td>
 * </tr>
 * </table>
 *<p>
 * Example:<br>
 * 
 * <pre>
 * 	&lt;Array id=&quot;somethings&quot; type=&quot;mr.go.yaxc.tests.test2.Something&quot;&gt;
 * 		&lt;Method name=&quot;mr.go.yaxc.tests.test2.FactoryOfSomething.createSomething&quot; /&gt;
 * 		&lt;Method name=&quot;mr.go.yaxc.tests.test2.FactoryOfSomething.createSomething&quot; /&gt;
 * 		&lt;Method name=&quot;mr.go.yaxc.tests.test2.FactoryOfSomething.createSomething&quot; /&gt;
 * 		&lt;Method name=&quot;mr.go.yaxc.tests.test2.FactoryOfSomething.createSomething&quot; /&gt;
 * 	&lt;/Array&gt;
 * </pre>
 * 
 * </p>
 * <h6>IMPORTANT:</h6>If some param could not be stored in created array,
 * runtime exception occurs
 * 
 * 
 * @author Marcin Rzeźnicki
 * 
 */
public class CreateArray extends Action {

    private Class<?> arrayComponentType;
    private String id;

    @Override
    public void carryOut(String name, Attributes attributes,
                         ScopeHandler handler)
            throws Exception {
        final String componentTypename = attributes.getValue(
                getAttribute("type"));
        if (componentTypename == null) {
            throw new ConfigurationError("Missing attribute: " + getAttribute(
                    "type"));
        }
        id = attributes.getValue(getAttribute("id"));
        arrayComponentType = Class.forName(componentTypename);
        params = new LinkedList<Object>();
    }

    @Override
    public void processed(String body, ScopeHandler handler)
            throws Exception {
        Object array = Array.newInstance(arrayComponentType,
                                         params.size());
        int idx = 0;
        for (Object param : params) {
            Array.set(array,
                      idx++,
                      param);
        }
        if (id != null) {
            handler.setReference(id,
                                 array);
            if (handler.getLogger().isDebugEnabled()) {
                handler.getLogger().debug("Id '{}': {}",
                                          id,
                                          Arrays.toString((Object[]) array));
            }
        }
        handler.tryAddParam(array);
    }
}
